package com.lkx.sms.data;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import android.content.Context;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SmsSessionListAdapter extends BaseAdapter {

	private Context context;
	private List<SmsSession> smsSessions;
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
	private Date date = new java.util.Date();


	public SmsSessionListAdapter(Context context ,List<SmsSession> smsSessions) {
		super();
		this.context = context;
		this.smsSessions = smsSessions;
	}

	@Override
	public int getCount() {
		return smsSessions.size();
	}

	@Override
	public Object getItem(int position) {
		return smsSessions.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		SmsSession smsSession = smsSessions.get(position);
		return new SmsSessionListView(context, smsSession);
	}

	private final class SmsSessionListView extends LinearLayout {

		public SmsSessionListView(Context context, SmsSession smsSession) {
			super(context);
			setOrientation(LinearLayout.VERTICAL);
			LinearLayout smsHeaderViewGroup = new LinearLayout(context);
			smsHeaderViewGroup.setOrientation(LinearLayout.HORIZONTAL);
			TextView smsBodyView = new TextView(context);
			//make the sms content display in single line and truncate the extra content
			smsBodyView.setSingleLine();
			smsBodyView.setEllipsize(TruncateAt.END);
			smsBodyView.setText(smsSession.getSmsContents().get(smsSession.getSmsContents().size()-1).getBody());
			LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			addView(smsHeaderViewGroup, params1);
			addView(smsBodyView,params1);

			TextView addressView = new TextView(context);
			TextView dateView = new TextView(context);
			StringBuilder addressStrB = new StringBuilder(smsSession.getAddress()).append("(").append(smsSession.getSmsContents().size()).append(")");
			addressView.setText(addressStrB.toString());
			date.setTime(smsSession.getLastUpdate());
			String dateStr = sdf.format(date);
			dateView.setText(dateStr);
			LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			params2.gravity = Gravity.LEFT;
			LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			params3.gravity = Gravity.RIGHT;
			smsHeaderViewGroup.addView(addressView, params2);
			smsHeaderViewGroup.addView(dateView, params3);
		}

	}

}
